<template>
  <view class="goodsDetial" v-if="goodsDetail.goods_price">
    <!-- 轮播图区域 -->
    <swiper class="myswiper" indicator-dots autoplay circular interval="2000" duration="500">
      <swiper-item v-for="(item, index) in goodsDetail.pics" :key="index">
        <image @click="preImg(index)" class="myimg" :src="item.pics_big" mode=""></image>
      </swiper-item>
    </swiper>

    <!-- 商品信息区域 -->
    <view class="info">
      <view class="price">
        ￥{{ goodsDetail.goods_price }}
      </view>
      <view class="name">
        <view class="left">
          {{ goodsDetail.goods_name }}
        </view>
        <view class="right">
          <uni-icons type="star" size="20"></uni-icons>
          收藏
        </view>
      </view>
      <view class="foot">
        快递：免运费
      </view>
    </view>

    <!-- 商品详情 -->
    <view class="title">
      - 商品详情 -
    </view>
    <rich-text :nodes="goodsDetail.goods_introduce"></rich-text>

    <!-- 商品导航 -->
    <view class="goodsnav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="btnClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    data() {
      return {
        goodsDetail: {},
        goods_id: '',
        options: [{
          icon: 'headphones',
          text: '客服'
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    onLoad(options) {
      // 接收商品id
      this.goods_id = options.goods_id
      // 获取所有商品详情
      this.getGoodsDetail()
    },
    computed: {
      ...mapGetters('cart', ['total'])
    },
    watch: {
      total: {
        handler(newVal) {
          this.options[1].info = newVal
        },
        immediate: true
      }
    },
    methods: {
      ...mapMutations({
        addToCart: 'cart/addToCart'
      }),
      // 点击右边按钮事件
      btnClick(e) {
        console.log('点击了右边按钮的内容', e)
        // 判断点击了哪一个按钮
        if (e.content.text === '加入购物车') {
          // 生成要添加的商品信息
          const goodsObj = {
            goods_id: this.goodsDetail.goods_id,
            goods_name: this.goodsDetail.goods_name,
            goods_price: this.goodsDetail.goods_price,
            goods_small_logo: this.goodsDetail.goods_small_logo,
            goods_status:true,
            goods_count: 1
          }
          // 调用addToCart方法
          this.addToCart(goodsObj)
          // 提示
          uni.showToast({
            icon: 'success',
            title: '添加购物车成功'
          })
        }
      },
      // 点击左侧图标内容
      onClick(e) {
        console.log('点击进入左侧内容部分', e);
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      preImg(index) {
        // 得到图片数组
        const imgUrls = this.goodsDetail.pics.map(item => {
          return item.pics_big
        })
        uni.previewImage({
          current: index,
          urls: imgUrls
        })
      },
      async getGoodsDetail() {
        const {
          data: res
        } = await uni.$http.get(`/api/public/v1/goods/detail?goods_id=${this.goods_id}`)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.goodsDetail = res.message
        this.goodsDetail.goods_introduce = this.goodsDetail.goods_introduce.replace(/<img /g,
          '<img style="display:block;"').replace(/webp/g, 'jpg')
      }
    }
  }
</script>

<style lang="scss">
  .goodsDetial {
    .myswiper {
      width: 100%;
      height: 750rpx;

      .myimg {
        width: 100%;
        height: 750rpx;
      }
    }

    .info {
      .price {
        font-size: 30px;
        color: red;
        margin: 10px;
      }

      .name {
        display: flex;
        align-items: center;
        padding: 10px;

        .left {
          word-break: break-all;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
          margin-right: 30px;
        }

        .right {
          width: 180px;
          display: flex;
          flex-direction: column;
          align-items: center;
          font-size: 12px;
          border-left: 1px solid #ccc;
        }
      }

      .foot {
        margin: 10px;
        font-size: 14px;
        color: #666;
      }
    }

    .title {
      font-size: 18px;
      font-weight: 700;
      text-align: center;
      padding: 20px;
    }

    .goodsnav {
      width: 100%;
      position: fixed;
      bottom: 0;
      left: 0;
    }
  }
</style>
